import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    name: "list",
    component: () => import("../views/ListView.vue"),
  },
  {
    path: "/detail/:id",
    name: "detail",
    component: () => import("../views/DetailView.vue"),
  },
  {
    path: "/shoppingcar",
    name: "shoppingcar",
    component: () => import("../views/ShoppingCar.vue"),
    meta:{
      isLogin:true
    }
  },
  {
    path: "/mine",
    name: "mine",
    component: () => import("../views/MineView.vue"),
  },
  {
    path: "/pay",
    name: "pay",
    component: () => import("../views/PayView.vue"),
  },
  {
    path: "/address",
    name: "address",
    component: () => import("../views/AddressView.vue"),
    meta:{
      isLogin:true
    }
  },
];

const router = new VueRouter({
  routes,
});

router.beforeEach((to,from,next)=>{
  if(to.meta&&to.meta.isLogin){
    let  userinfo  = window.localStorage.getItem("userinfo");
    if(userinfo){
      next()
    }else{
      window.localStorage.setItem("backUrl",to.path);
      next("/mine");
    }
  }
  next();
})

export default router;
